package jmine.tec.web.front.pages.requests.filter;

import java.math.BigDecimal;

import jmine.tec.web.front.domain.BackOfficeRequestPropertyBigDecimal;
import jmine.tec.web.front.domain.BackOfficeRequestPropertyDate;
import jmine.tec.web.front.domain.BackOfficeRequestValue;
import jmine.tec.web.wicket.component.MapsWicketDateTextField;
import jmine.tec.web.wicket.converter.BigDecimalConverter;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.convert.IConverter;
import org.hibernate.Hibernate;

/**
 * Base panel for property filter search
 * 
 * @author israel.crisanto
 */
public class BackOfficeRequestPropertyPanel extends Panel {

    private BackOfficeRequestFilter backOfficeRequestFilter;

    private PropertyListView<BackOfficeRequestValue<?>> listView;

    /**
     * Constructor
     * 
     * @param id panel id
     * @param filter filter
     */
    public BackOfficeRequestPropertyPanel(String id, BackOfficeRequestFilter filter) {
        super(id);
        this.backOfficeRequestFilter = filter;
        this.createComponents();
    }

    /**
     * Create search form components from {@link BackOfficeRequestFilter} properties
     */
    private void createComponents() {

        this.listView =
                new PropertyListView<BackOfficeRequestValue<?>>("propertyValues", this.backOfficeRequestFilter.getPropertyValues()) {
                    @Override
                    protected void populateItem(ListItem<BackOfficeRequestValue<?>> item) {
                        BackOfficeRequestValue<?> obj = item.getModelObject();
                        item.add(new Label("property.description"));

                        if (Hibernate.getClass(obj.getProperty()).equals(BackOfficeRequestPropertyBigDecimal.class)) {
                            item.add(new TextField<BigDecimal>("value") {
                                @Override
                                public IConverter getConverter(Class<?> type) {
                                    return new BigDecimalConverter();
                                }
                            });
                        } else if (Hibernate.getClass(obj.getProperty()).equals(BackOfficeRequestPropertyDate.class)) {
                            item.add(MapsWicketDateTextField.forDatePattern("value"));
                        } else {
                            item.add(new TextField<String>("value"));
                        }
                    }
                };

        this.add(this.listView);
    }
}
